/*
 * $Id: BaseObject.java 129 2009-07-09 06:25:26Z donbeave $
 */

package labour.concept.client.data;

import java.util.List;

import com.extjs.gxt.ui.client.store.ListStore;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class BaseObject extends XModel<ObjectModel> {

  private ListStore<ObjectText> textsStore;

  public BaseObject(ObjectModel model, ConceptManager manager) {
    super(model, manager);

    textsStore = new ListStore<ObjectText>();
  }

  public String getId() {
    return getModel().getId();
  }

  public ObjectType getType() {
    return getManager().getObjectTypeById(getModel().getTypeId());
  }

  public Boolean hasParent() {
    return (getModel().getParentId() != null && !getModel().getParentId().equals("0"));
  }

  public void loadParent(AsyncCallback<? extends BaseObject> callback) {
    getManager().loadObjectById(getModel().getParentId(), callback);
  }

  public void setParent(BaseObject parent) {
    getModel().set(ObjectModel.PARENT_ID, parent.getModel().getId());
  }

  public Boolean hasChildren() {
    return (getModel().getChildrenCount() > 0);
  }

  public int getChildrenCount() {
    return getModel().getChildrenCount();
  }

  public void loadChildren(AsyncCallback<List<BaseObject>> callback) {
    // TODO подгрузка подобъектов
  }

  public void loadBlocks(AsyncCallback<List<?>> callback) {
    // TODO подгрузка/перегрузка блоков объекта
  }

  public Boolean hasData(Locale locale) {
    return (textsStore.findModel(ObjectTextModel.LOCALE_ID, locale.getModel().getId()) != null);
  }

  public Boolean hasData() {
    return hasData(getManager().getDefaultLocale());
  }

  public ObjectText getData(Locale locale) {
    return textsStore.findModel(ObjectTextModel.LOCALE_ID, locale.getModel().getId());
  }

  public ObjectText getData() {
    return getData(getManager().getDefaultLocale());
  }

  public ObjectText getData(String language) {
    Locale locale = getManager().getLocaleByLanguage(language);
    return getData(locale);
  }

  public Boolean isExist() {
    return (getModel().getId() != null);
  }

  public BaseObject createChild(ObjectType type, String name, Locale locale) {
    BaseObject child = getManager().createObject(type, name, locale);
    child.setParent(this);

    return child;
  }

  public boolean extendData(Locale locale, String name) {
    if (hasData(locale)) {
      getData(locale).getModel().setName(name);

      return false;
    }

    ObjectText text = getManager().createText(this, locale, name);
    textsStore.add(text);

    return true;
  }

  public void save(AsyncCallback<? extends BaseObject> callback) {
    getManager().saveObject(this, callback);
  }

  public void delete(AsyncCallback<? extends BaseObject> callback) {
    getManager().deleteObject(this, callback);
  }

  protected ListStore<ObjectText> getTextsStore() {
    return textsStore;
  }

  protected BaseObject getParent() {
    return getManager().getObjectById(getModel().getParentId());
  }

}
